package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.dao.OrderDao;
import com.atguigu.springcloud.domain.CommonResult;
import com.atguigu.springcloud.domain.Order;
import com.atguigu.springcloud.service.AccountService;
import com.atguigu.springcloud.service.OrderService;
import com.atguigu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author 赵赳
 * @CreateTime: 2021/3/5 20:19
 * @Description:
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private StorageService storageService;
    @Autowired
    private AccountService accountService;

    @Override
    @GlobalTransactional
    public void create(Order order) {
        log.info("订单开始生成开始");
        orderDao.create(order);

        /**
         * 调用storageService接口
         */
        log.info("库存减少开始");
        CommonResult result = storageService.decrease(order.getProductId(), order.getCount());
        log.info("库存减少结束");

        /**
         * 调用accountService接口
         */
        log.info("减少账户余额开始");
        CommonResult result1 = accountService.decrease(order.getUserId(), order.getMoney());
        log.info("减少账户余额结束");

        //修改订单状态
        log.info("减少订单开始");

        orderDao.update(order.getUserId(),0);
        log.info("减少订单结束");

        log.info("订单已经生成");
    }

}
